//
//  ViewController.m
//  sss
//
//  Created by mac on 2021/3/16.
//

#import "ViewController.h"
@import Flutter;
@import AFNetworking;
@import SSZipArchive;

@interface ViewController ()
@property FlutterEngine *engin;

@end

@implementation ViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSString *destPath = [NSString stringWithFormat:@"%@/Documents/mkk",NSHomeDirectory()];
//    NSString *bundlePath = [NSString stringWithFormat:@"%@/flutterBundle.bundle",destPath];
    NSString *bundlePath = [NSString stringWithFormat:@"%@/mk7.bundle",destPath];
    bundlePath = [[NSBundle mainBundle] pathForResource:@"mk7" ofType:@"bundle"];

//    NSString *bundlePath = [NSString stringWithFormat:@"%@/Documents/mk8.bundle",NSHomeDirectory()];
//
//    [NSTimer scheduledTimerWithTimeInterval:1 repeats:NO block:^(NSTimer * _Nonnull timer) {
//        [self showFlutterWithBundlePath:bundlePath];
//    }];
//
    
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"mkk" ofType:@"txt"];
//    NSString *path1 = [NSString stringWithFormat:@"%@/Documents/mkk/mk7.bundle/mkk.txt",NSHomeDirectory()];
//    NSError *error;
//    [[NSFileManager defaultManager] copyItemAtPath:path toPath:path1 error:&error];
//    NSLog(@"%@",error);
//    NSLog(@"%@",NSHomeDirectory());
    [self downloadBundle];
}

-(void) downloadBundle{
   NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@"https://mmm-1254409884.cos.ap-guangzhou.myqcloud.com/mk7.bundle.zip"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"下载成功:%f",data.length/1000000.0);
        if (error) {
            NSLog(@"error:%@",error);
        }

        //解压
        NSString *zipPath = [NSString stringWithFormat:@"%@/Documents/a.zip",NSHomeDirectory()];
        NSString *destPath = [NSString stringWithFormat:@"%@/Documents/mkk",NSHomeDirectory()];
        [data writeToFile:zipPath options:0 error:nil];
        sleep(2);
        //解压文件
        BOOL isSuccess = [SSZipArchive unzipFileAtPath:zipPath toDestination:destPath];
        NSLog(@"解压完成：%d",isSuccess);
        NSLog(@"%@",destPath);
       [[NSOperationQueue mainQueue] addOperationWithBlock:^{
           NSString *bundlePath = [NSString stringWithFormat:@"%@/mk7.bundle",destPath];
           [self showFlutterWithBundlePath:bundlePath];
       }];
    }];
    [task resume];
}

-(void) showFlutterWithBundlePath:(NSString *)bundlePath{
    [self dismissViewControllerAnimated:YES completion:nil];
    sleep(1);
    NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
    FlutterDartProject *project = [[FlutterDartProject alloc] initWithPrecompiledDartBundle:bundle];
    self.engin = [[FlutterEngine alloc] initWithName:@"fsfsf" project:project];
    [self.engin run];
    
  FlutterViewController *flutterViewController =
      [[FlutterViewController alloc] initWithEngine:self.engin nibName:nil bundle:nil];
  [self presentViewController:flutterViewController animated:YES completion:nil];
}

- (IBAction)a:(id)sender {
    [self showFlutter];
    [NSTimer scheduledTimerWithTimeInterval:3 repeats:NO block:^(NSTimer * _Nonnull timer) {
        [self dismiss];
    }];
}
- (IBAction)b:(id)sender {
    [self showFlutter2];
    [NSTimer scheduledTimerWithTimeInterval:3 repeats:NO block:^(NSTimer * _Nonnull timer) {
        [self dismiss];
    }];
}
-(void) dismiss{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)showFlutter {
    [self dismissViewControllerAnimated:YES completion:nil];
    sleep(1);
    NSString *path = [NSString stringWithFormat:@"%@/Documents/mk3.bundle",NSHomeDirectory()];
    NSBundle *bundle = [NSBundle bundleWithPath:path];
    FlutterDartProject *project = [[FlutterDartProject alloc] initWithPrecompiledDartBundle:bundle];
    self.engin = [[FlutterEngine alloc] initWithName:@"fsfsf" project:project];
    [self.engin run];
    
  FlutterViewController *flutterViewController =
      [[FlutterViewController alloc] initWithEngine:self.engin nibName:nil bundle:nil];
  [self presentViewController:flutterViewController animated:YES completion:nil];
}
- (void)showFlutter2 {
    [self dismissViewControllerAnimated:YES completion:nil];
    sleep(1);
    NSString *path = [NSString stringWithFormat:@"%@/Documents/flutterBundle.bundle",NSHomeDirectory()];
    NSBundle *bundle = [NSBundle bundleWithPath:path];
    FlutterDartProject *project = [[FlutterDartProject alloc] initWithPrecompiledDartBundle:bundle];
    self.engin = [[FlutterEngine alloc] initWithName:@"fsfsf" project:project];
    [self.engin run];
    
  FlutterViewController *flutterViewController =
      [[FlutterViewController alloc] initWithEngine:self.engin nibName:nil bundle:nil];
  [self presentViewController:flutterViewController animated:YES completion:nil];
}

@end
